//
//  WhoScanViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/11/21.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "WhoScanViewController.h"

@interface WhoScanViewController ()
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,strong)NSArray *contentArray;
@property(nonatomic,strong)NSMutableArray *selectArray;
@property(nonatomic,assign)BOOL  OtherSelect;

@property(nonatomic,assign)NSInteger  currentTitleindex;
@end

@implementation WhoScanViewController
- (NSMutableArray *)selectArray
{
    if (!_selectArray) {
        _selectArray=[NSMutableArray array];
    }
    return _selectArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.OtherSelect=false;
    
    if ([[JHUserInfo getUserInfo].role_type isEqualToString:@"1"]) {
        self.titleArray=@[@"公开",@"私密",@"部分"];
        self.contentArray=@[@"班级",@"家长",@"老师"];
    }else{
        self.titleArray=@[@"公开",@"私密"];
    }
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem rightBarButtonItemWithTitle:@"完成" target:self selector:@selector(finish)];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return self.titleArray.count;
    }
    else if (section&self.OtherSelect)
        return self.contentArray.count;
    else
        return 0;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake( kScreenW - 20 - 25, 12, 20, 20)];
        imageview.tag = 1000;
        imageview.contentMode=UIViewContentModeScaleAspectFit;
        [cell.contentView addSubview:imageview];
    }
    if (indexPath.section) {
        cell.textLabel.text=self.contentArray[indexPath.row];
        cell.indentationLevel=3;
        
        
        UIImageView *imageview=[cell.contentView viewWithTag:1000];
        imageview.image=[self.selectArray containsObject:self.contentArray[indexPath.row]]?[UIImage imageNamed:@"select"]:[UIImage imageNamed:@"unselect"];
        
    }
    else{
        cell.textLabel.text=self.titleArray[indexPath.row];
        UIImageView *imageview=[cell.contentView viewWithTag:1000];
        imageview.image=self.currentTitleindex==indexPath.row?[UIImage imageNamed:@"SolidCircle"]:[UIImage imageNamed:@"HollowCircle"];
//        cell.accessoryView=imageview;
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!indexPath.section) {
        self.currentTitleindex=indexPath.row;
        self.OtherSelect=self.currentTitleindex==2;
        [self.tableView reloadData];
    }
    else
    {
        NSString *name=self.contentArray[indexPath.row];
        [self.selectArray containsObject:name]?[self.selectArray removeObject:name]:[self.selectArray addObject:name];
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
}
#pragma mark - 完成筛选
- (void)finish{
    // 0：所有人 1仅自己 2 老师 3 学生 4家长
    if (self.currentTitleindex<2) {
        if (self.selectWhoScanBlock) {
            self.selectWhoScanBlock([NSString stringWithFormat:@"%ld",(long)self.currentTitleindex]);
        }
    }
    else
    {
        NSMutableArray *selectindex=[NSMutableArray array];
        for (NSString *name in self.selectArray) {
            NSUInteger select = [self.contentArray indexOfObject:name];
            [selectindex addObject: @(select != 2 ? (select + 3):select)];
        }
        if (self.selectWhoScanBlock) {
            self.selectWhoScanBlock([selectindex componentsJoinedByString:@","]);
        }
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
